package com.itheima.day30.demo03;

import com.itheima.day30.demo02.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import sun.java2d.pipe.SpanIterator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class Test {
    @org.junit.Test
    public void show1() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select *from student;";
        try {
            Object[] query = queryRunner.query(sql, new ArrayHandler());
            System.out.println(Arrays.toString(query));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void show2() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select *from student;";
        try {
            final List<Object[]> query = queryRunner.query(sql, new ArrayListHandler());
            for (Object[] objects : query) {
                System.out.println(Arrays.toString(objects));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void show3() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select *from student;";
        try {
            final Map<String, Object> query = queryRunner.query(sql, new MapHandler());
            System.out.println(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void show4() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select *from student;";
        try {
            final List<Map<String, Object>> query = queryRunner.query(sql, new MapListHandler());
            for (Map<String, Object> map : query) {
                System.out.println(query);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void show5() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select *from student;";
        try {
            final Studnet studnet = queryRunner.query(sql, new BeanHandler<>(Studnet.class));
            System.out.println(studnet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void show6() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select *from student;";
        try {
            final List<Studnet> studnetList = queryRunner.query(sql, new BeanListHandler<>(Studnet.class));
            for (Studnet studnet : studnetList) {
                System.out.println(studnet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void show7() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select *from student;";
        try {
            final List<Object> list = queryRunner.query(sql, new ColumnListHandler("name"));
            for (Object o : list) {
                System.out.println(o);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void show8() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select sum(age) from student;";
        try {
            final Object query = queryRunner.query(sql, new ScalarHandler());
            System.out.println(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test
    public void show9() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from student;";
        try {
            final Map<Object, Map<String, Object>> query = queryRunner.query(sql, new KeyedHandler("name"));
            for (Map.Entry<Object, Map<String, Object>> entry : query.entrySet())
                System.out.println(entry.getKey() + ":" + entry.getValue());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
